package com.b2b.controller;



import com.b2b.model.EvaluateDto;

import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.Orders;

import com.b2b.model.entity.goods.Evaluate;
import com.b2b.service.CompanyInfoService;
import com.b2b.service.EvaluateService;
import com.b2b.service.OrdersService;
import com.b2b.service.ShopService;
import com.b2b.service.goodsService.GoodsService;
import com.b2b.util.EvaluateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/evaluate")
@CrossOrigin
public class EvaluateController {


    @Autowired
    EvaluateService evaluateService;

    @Autowired
    EvaluateUtil evaluateUtil;

    @Autowired
    GoodsService goodsService;

    @Autowired
    CompanyInfoService companyInfoService;

    @Autowired
    ShopService shopService;

    @Autowired
    OrdersService ordersService;



    @PostMapping("/add")
    public ResponseBean add(@RequestBody EvaluateDto evaluateDto){
        evaluateService.insert(evaluateDto);
        return new ResponseBean(HttpStatus.OK.value(), "新增成功", null);
    }

    @DeleteMapping("/delete")
    public ResponseBean delete(@RequestParam Integer evaluateId){
        evaluateService.deleteByPrimaryKey(evaluateId);
        return new ResponseBean(HttpStatus.OK.value(), "删除成功", null);
    }

    @PutMapping("/update")
    public ResponseBean update(@RequestBody EvaluateDto evaluateDto){
        evaluateService.updateByPrimaryKeySelective(evaluateDto);
        return new ResponseBean(HttpStatus.OK.value(), "修改成功", null);
    }

    @GetMapping("/getMyBought")
    public ResponseBean getMyBought(@RequestParam Map<String, Object> map){
        int page = 1, rows = 10;
        if(map.containsKey("page"))
            page = Integer.parseInt(map.get("page").toString());
        if(map.containsKey("rows"))
            rows = Integer.parseInt(map.get("rows").toString());
        PageHelper.startPage(page, rows);
        Integer buyerId = Integer.parseInt(map.get("buyerId").toString());
        Orders orders = new Orders();
        orders.setBuyerId(buyerId);
        List<Orders> ordersList = ordersService.select(orders);

        List<EvaluateDto> evaluateList = new ArrayList<>();

        for (Orders o:ordersList
             ) {
            Evaluate evaluate = new Evaluate();
            evaluate.setOrderId(o.getOrderId());
            List<Evaluate> temp = evaluateService.select(evaluate);

            for (Evaluate e:temp
                 ) {
                e.setShopId(o.getShopId());
                EvaluateDto evaluateDto = evaluateUtil.getEvaluateDto(e);
                evaluateDto.setShopName(shopService.selectByPrimaryKey(o.getShopId()).getShopName());
                evaluateDto.setGoodsName(goodsService.selectByPrimaryKey(e.getGoodsId()).getGoodsName());
                evaluateList.add(evaluateDto);
            }
        }

        PageInfo<EvaluateDto> selectPage = new PageInfo<>(evaluateList);
        Map<String, Object> result = new HashMap<>();
        result.put("count", selectPage.getTotal());
        result.put("evaluateList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

    @GetMapping("/getMySold")
    public ResponseBean getMySold(@RequestParam Map<String, Object> map){
        int page = 1, rows = 10;
        if(map.containsKey("page"))
            page = Integer.parseInt(map.get("page").toString());
        if(map.containsKey("rows"))
            rows = Integer.parseInt(map.get("rows").toString());
        PageHelper.startPage(page, rows);
        Integer sellerId = Integer.parseInt(map.get("sellerId").toString());
        Orders orders = new Orders();
        orders.setSellerId(sellerId);
        List<Orders> ordersList = ordersService.select(orders);

        List<EvaluateDto> evaluateList = new ArrayList<>();

        for (Orders o:ordersList
        ) {
            Evaluate evaluate = new Evaluate();
            evaluate.setOrderId(o.getOrderId());
            List<Evaluate> temp = evaluateService.select(evaluate);

            for (Evaluate e:temp
            ) {
                EvaluateDto evaluateDto = evaluateUtil.getEvaluateDto(e);
                evaluateDto.setBuyerId(o.getBuyerId());
                evaluateDto.setBuyerName(companyInfoService.selectByPrimaryKey(o.getBuyerId()).getCompanyName());
                evaluateDto.setGoodsName(goodsService.selectByPrimaryKey(e.getGoodsId()).getGoodsName());

                evaluateList.add(evaluateDto);
            }
        }

        PageInfo<EvaluateDto> selectPage = new PageInfo<>(evaluateList);
        Map<String, Object> result = new HashMap<>();
        result.put("count", selectPage.getTotal());
        result.put("evaluateList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);

    }

    @GetMapping("/getGoods")
    public ResponseBean getGoods(@RequestParam  Map<String, Object> map){
        int page = 1, rows = 10;
        if(map.containsKey("page"))
            page = Integer.parseInt(map.get("page").toString());
        if(map.containsKey("rows"))
            rows = Integer.parseInt(map.get("rows").toString());
        PageHelper.startPage(page, rows);
        Integer goodsId = Integer.parseInt(map.get("goodsId").toString());
        Evaluate evaluate = new Evaluate();
        evaluate.setGoodsId(goodsId);
        List<Evaluate> evaluateList =  evaluateService.select(evaluate);
        List<EvaluateDto> evaluateDtoList = new ArrayList<>();
        Orders orders = new Orders();
        for (Evaluate e:evaluateList
             ) {
            orders.setOrderId(e.getOrderId());
            orders = ordersService.selectByPrimaryKey(orders.getOrderId());
            EvaluateDto evaluateDto = evaluateUtil.getEvaluateDto(e);
            evaluateDto.setBuyerId(orders.getBuyerId());
            evaluateDto.setBuyerName(companyInfoService.selectByPrimaryKey(orders.getBuyerId()).getCompanyName());

            evaluateDtoList.add(evaluateDto);
        }

        PageInfo<EvaluateDto> selectPage = new PageInfo<>(evaluateDtoList);
        Map<String, Object> result = new HashMap<>();
        result.put("count", selectPage.getTotal());
        result.put("evaluateList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);

    }


}
